<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >

<!-- @author Zhao,XingGuo -->
<!-- @version 2010-01-06 09:17 -->
<sqlMap namespace="${table.tableName}">

	<typeAlias alias="${table.objectName}" type="com.${companyName}.${projectName}.domain.${table.className}" />

	<cacheModel id="oneDayCache" type="OSCACHE">
		<flushInterval hours="24" />
		<flushOnExecute statement="insert${table.className}" />
		<flushOnExecute statement="update${table.className}" />
		<flushOnExecute statement="delete${table.className}" />
	</cacheModel>

	<resultMap id="${table.objectName}ResultForList" class="${table.objectName}">
	<#list fieldList as f >
		<result column="${f.culumnName}" property="${f.objectName}" jdbcType="${f.jdbcType}" />
	</#list>
	</resultMap>

	<resultMap id="${table.objectName}Result" class="${table.objectName}" extends="${table.objectName}ResultForList">
	</resultMap>

	<sql id="sf-${table.objectName}">
	<#list fieldList as f >
		<isNotEmpty prepend=" and " property="${f.objectName}">t.${f.culumnName} = #${f.objectName}:${f.jdbcType}#</isNotEmpty>
	</#list>
	</sql>

	<select id="select${table.className}" resultMap="${table.objectName}Result" parameterClass="${table.objectName}" cacheModel="oneDayCache">
		select 	
		<#list fieldList as f >
			t.${f.culumnName}<#if f_has_next>,</#if>
		</#list> 
	 from ${table.tableName} t where 1 = 1
		<include refid="sf-${table.objectName}" />
	</select>

	<select id="select${table.className}List" resultMap="${table.objectName}ResultForList" parameterClass="${table.objectName}" cacheModel="oneDayCache">
		<isNotEmpty property="row.count">
			<![CDATA[ select * from ( ]]>
		</isNotEmpty>
		select 		
		<#list fieldList as f >
			t.${f.culumnName}<#if f_has_next>,</#if>
		</#list> 
		from ${table.tableName} t where 1 = 1
		<include refid="sf-${table.objectName}" />
		<!-- order by ID asc -->
		<isNotEmpty property="row.count">
			<![CDATA[ ) where rownum <= #row.count# ]]>
		</isNotEmpty>
	</select>

	<select id="select${table.className}Count" resultClass="long" parameterClass="${table.objectName}" cacheModel="oneDayCache">
		select count('1') from ${table.tableName} t where 1 = 1
		<include refid="sf-${table.objectName}" />
	</select>

	<select id="select${table.className}PaginatedList" resultMap="${table.objectName}Result" parameterClass="${table.objectName}" cacheModel="oneDayCache">
		<![CDATA[ select * from ( select t_.*, rownum rn_ from ( ]]>
		select 
		<#list fieldList as f >
			t.${f.culumnName}<#if f_has_next>,</#if>
		</#list> 
		 from ${table.tableName} t where 1 = 1
		<include refid="sf-${table.objectName}" />
		<!-- order by ID asc -->
		<![CDATA[ ) t_ where rownum <= (#row.first# + #row.count#)) where rn_ >= (#row.first# + 1) ]]>
	</select>

	<insert id="insert${table.className}" parameterClass="${table.objectName}">
		<selectKey resultClass="long" keyProperty="id">select seq_xxx.nextval as id from dual</selectKey>
		<![CDATA[insert into ${table.tableName} (]]>
		<dynamic prepend=" ">
		<#list fieldList as f >
			<isNotNull prepend="," property="${f.objectName}">${f.culumnName}</isNotNull>	
		</#list>
		</dynamic>
		<![CDATA[) values (]]>
		<dynamic prepend=" ">
		<#list fieldList as f >
			<isNotNull prepend="," property="${f.objectName}">#${f.objectName}:${f.jdbcType}#</isNotNull>
		</#list>
		</dynamic>
		<![CDATA[)]]>
	</insert>

	<update id="update${table.className}" parameterClass="${table.objectName}">
		update ${table.tableName}
		<dynamic prepend="set">
		<#list fieldList as f >
			<isNotNull prepend="," property="${f.objectName}">${f.culumnName} = #${f.objectName}:${f.jdbcType}#</isNotNull>
		</#list>
		</dynamic>
		where
		<isNotEmpty prepend=" " property="id">ID = #id#</isNotEmpty>
		<isEmpty prepend=" " property="id">
			<isNotEmpty prepend=" " property="map.pks">
				ID in
				<iterate close=")" open="(" conjunction="," property="map.pks">#map.pks[]#</iterate>
			</isNotEmpty>
		</isEmpty>
	</update>

	<delete id="delete${table.className}" parameterClass="${table.objectName}">
		delete from ${table.tableName} where
		<isNotEmpty prepend=" " property="id">ID = #id#</isNotEmpty>
		<isEmpty prepend=" " property="id">
			<isNotEmpty prepend=" " property="map.pks">
				ID in
				<iterate close=")" open="(" conjunction="," property="map.pks">#map.pks[]#</iterate>
			</isNotEmpty>
		</isEmpty>
	</delete>

</sqlMap>